package com.pixiv.controller;

import cn.hutool.json.JSONObject;
import com.pixiv.Entity.AuditTitlePicture;
import com.pixiv.Entity.Notice;
import com.pixiv.Entity.User;
import com.pixiv.service.AuditTitleService;
import com.pixiv.service.NoticeService;
import com.pixiv.service.TitleService;
import com.pixiv.service.UserService;
import com.pixiv.tools.SendPack;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;


@Controller
@RequestMapping("/back")
public class BackController
{

    @Autowired
    private AuditTitleService auditTitleService;
    @Autowired
    private NoticeService noticeService;
    @Autowired
    private UserService userService;
    @Autowired
    private TitleService titleService;

    @ResponseBody
    @RequestMapping("getAuditTitleTable")
    public String getAuditTitleTable(HttpServletRequest req, HttpServletResponse resp){

        List<AuditTitlePicture> al = auditTitleService.getAduitTitlePictures();
        JSONObject jsonObject = new JSONObject();
        jsonObject.accumulate("count",10);
        jsonObject.accumulate("code",0);
        jsonObject.accumulate("msg","null");
        jsonObject.accumulate("data",al);
        //jsonObject.accumulate("data",)
        System.out.println(jsonObject.toString());
        return jsonObject.toString();
    }



    @RequestMapping("gotoBack")
    public ModelAndView gotoBack(){
        return new ModelAndView("/pixiv/manager/back_new");
    }
    @RequestMapping("/gotoAuditTitlePicture")
    public ModelAndView gotoAudit(){
        return new ModelAndView("/pixiv/manager/auditPicture");
    }

    @RequestMapping("/gotoNoticeManager")
    public ModelAndView gotoNoticeManager(){
        return new ModelAndView("/pixiv/manager/noticeManager");
    }

    @RequestMapping("/gotoRegisterSet")
    public ModelAndView gotoRegisterSet(){
        return new ModelAndView("/pixiv/manager/setRegisterPicture");
    }

    @RequestMapping("/welcome")
    public ModelAndView welcome(){
        return new ModelAndView("/pixiv/manager/welcome");
    }

    @RequestMapping("/backToPosition")
    public ModelAndView gotoPosition(){
        return new ModelAndView("/pixiv/index");
    }
    //进入加入新公告
    @RequestMapping("addNotice")
    public ModelAndView addNotice(){
        return new ModelAndView("pixiv/manager/addNotice");
    }

    @RequestMapping("gotoTitleLst")
    public ModelAndView gotoTitleLst(){
        return new ModelAndView("pixiv/manager/titlelst");
    }

    @ResponseBody
    @RequestMapping("putNotice")
    public void putNotice(HttpServletRequest req, HttpServletResponse resp)
    {

        String caption = req.getParameter("caption");
        String context = req.getParameter("context");

        HttpSession hs = req.getSession();
        User u = (User) hs.getAttribute("GLOBAL_USER_OBJECT");

        String uuid = UUID.randomUUID().toString();
        Notice n = new Notice();
        n.setNoticeCaption(caption);
        n.setNoticeContext(context);
        n.setNoticeid(uuid);
        n.setNoticeUserID(u.getUserID() );
        noticeService.insertNotice(n);
        SendPack.flag(resp,1);
    }

    @RequestMapping("showNotice")
    public ModelAndView toNotice(String id, HttpSession hs){

        User u = (User) hs.getAttribute("GLOBAL_USER_OBJECT");
        u.getVar().clear();
        u.getVar().push_vector("Show_Notice_ID",id);
        //System.out.println(id);

        ModelAndView model = new ModelAndView();
        model.setViewName("pixiv/manager/showNotice");
        return model;
    }

    /**
     * 用户显示公告时，进行发送一次ajax请求
     * @param req
     * @param resp
     * @return
     */
    @ResponseBody
    @RequestMapping("flushNoticeToShowTitle")
    public String flushNoticeToShowTitle(HttpServletRequest req, HttpServletResponse resp)
    {
        HttpSession hs = req.getSession();
        User u = (User) hs.getAttribute("GLOBAL_USER_OBJECT");
        String uuid = (String) u.getVar().get_vector("Show_Notice_ID");

        Notice notice = noticeService.getNotice(uuid);
        JSONObject jsonObject = new JSONObject();
        jsonObject.accumulate("flag",1);
        jsonObject.accumulate("caption",notice.getNoticeCaption());
        jsonObject.accumulate("context",notice.getNoticeContext());

        int userid = notice.getNoticeUserID();
        User wirteUser = userService.getUser(userid);

        jsonObject.accumulate("username",wirteUser.getName() );

        return jsonObject.toString();

    }

    @ResponseBody
    @RequestMapping("deleteNotice")
    public void deleteNotice(HttpServletRequest req, HttpServletResponse resp)
    {
        String uuid = req.getParameter("id");
        noticeService.delNotice(uuid);
        SendPack.flag(resp,1);

    }

    @ResponseBody
    @RequestMapping("initBackAuditTitle")
    public String initBackAuditTitle(HttpServletRequest req, HttpServletResponse resp)
    {
        HttpSession hs = req.getSession();
        User u = (User) hs.getAttribute("GLOBAL_USER_OBJECT");
        String uuid = req.getParameter("uuid");
        //AuditTitlePicture atp = AuditTitleDAO.getListAduitTitlePictureByUUID(uuid);
        AuditTitlePicture auditTitlePicture = auditTitleService.getAuditTitle(uuid);
        System.out.println(auditTitlePicture);
        if (auditTitlePicture != null)
        {
            u.getVar().push_vector("GotoAuditObjectTargetPictureObj",auditTitlePicture);
            return SendPack.getFlag(1);
        }
        //GotoAuditObjectTargetPictureObj
        return SendPack.getFlag(0);

    }

    @RequestMapping("gotoBackAuditTitle")
    public ModelAndView gotoBackAuditTitle(HttpServletRequest req, HttpServletResponse resp)
    {
        return new ModelAndView("pixiv/titleshow/audittitle");
    }

    @ResponseBody
    @RequestMapping("deleteAuditTitle")
    public String deleteTitle(HttpServletRequest req, HttpServletResponse resp)
    {
        String uuid = req.getParameter("uuid");
        auditTitleService.delAuditTitle(uuid);
        return SendPack.getFlag(1);
    }

    @RequestMapping("registerpictureshow")
    public ModelAndView registerpictureshow(@RequestParam("id")int id){
        ModelAndView modelAndView = new ModelAndView("pixiv/manager/registerpictureshow");
        modelAndView.addObject("pictureID",id);
        return modelAndView;
    }

    @ResponseBody
    @RequestMapping("deleteTitle")
    public String deleteTitle(HttpServletRequest req){
        HttpSession hs = req.getSession();
        String uuid = req.getParameter("uuid");

        titleService.deleteTitle(uuid);
        titleService.deletePicture(uuid);
        return SendPack.getFlag(1);

    }

}
